﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mvc;
using APMS.WebSite.Models.DataStore;

namespace APMS.WebSite.Controllers
{
    [HandleError]
    public abstract class ProjectBaseController: BaseController
    {
        #region Keys for ViewData
        /// <summary>
        /// Used in action List of every derived Controller
        /// </summary>
        public const string CURRENT_PROJECT = "CurrentProject";
        #endregion

        /// <summary>
        /// Default action
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View("List");
        }

        protected Project GetProject(int projectId)
        {
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            var query =
                from project in db.Project
                where project.ProjectID == projectId
                select project;

            Project targetProject = null;
            if (query.Count() > 0)
            {
                targetProject = query.First();
            }
            else
            {
                throw new ApplicationException("Project is no longer exist");
            }

            return targetProject;
        }
    }
}
